*WHAT'S ACROSS THE BORDER?
*JHA, NEUMARK, AND RODRIGUEZ-LOPEZ

include directories.do


foreach u in "czonestate" {
* Load stacked pair data at the county or czone-state level
use "`datadir'`s'cbp_stacked_`u'pair_sample.dta", clear


************************************************
*******COUNTY-PAIRS OR MULTI-STATA CZONE PAIRS
************************************************

*** PAIR ID VARIABLE (EITHER CZONESTATEPAIR OR COUNTYPAIR)
local pair `u'pair


*Entity variable (EITHER CZONESTATE OR COUNTY)
local geo `u'


***********************************************
*MERGE EVENT VARIABLES
***********************************************

append using "`datadir'`s'stacked_`u'pair_lags.dta"
merge m:1 state year using  "`mwdir'`s'VZ_state_annual_event.dta"

keep if _merge==3
drop _merge


***********************************************
*GENERATE VARIABLES (SAME FOR COUNTY OR CZONE)
***********************************************


gen lmw=log(mw)

*Generate totempm, which removes emp of the industry of interest
foreach b in "emp" {
gen tot`b'm=tot`b'-`b'
}

foreach b in "emp" "wageap" {

gen l`b'=log(`b')
gen ltot`b'=log(tot`b')
gen ltot`b'm=log(tot`b'm)
}

gen lworkagepop=log(workagepop)


********************************************
**** DEFINE LOCAL VARIABLES
********************************************

local period year

*Dependent variable and regressors (SAME FOR COUNTY OR CZONE)

local mwvar lmw
local popvar lworkagepop
local wagevar lwageap
local empvar lemp
local totempvar ltotempm
local totwagevar ltotwageapm


**************************************************
****STACKED REGRESSIONS WITH REGHDFE
**************************************************

sort  `pair' `period'
 
gen state_a = statea
gen state_b = stateb

gen st_min = min(state_a, state_b)
gen st_max = max(state_a, state_b)
egen bordersegment = group(st_min st_max)
egen pairperiod=group(`pair' `period')

keep if `period'<=2016

*Timeseries data
egen panelid=group(`u'pair state)
tsset panelid year

*Specifications
foreach d in "I" {
forvalue h=4/7 {

local controls "`totempvar' `popvar'"

xtevent `empvar' `controls', pol(`d') w(`h') nofe note cluster(state bordersegment) reghdfe addabsorb(`u' pairperiod) 
xteventtest, allpre
xteventplot, nosupt scheme(s1color) scatterplotopts(mc(blue)) ciplotopts(lc(ltblue)) nozeroline yline(0, lpattern(dash) lstyle(refline) lc(black))
gr save "`outfigdir'`s'`d'_`h'", replace

}
}

gr combine "`outfigdir'`s'I_4.gph" "`outfigdir'`s'I_5.gph" "`outfigdir'`s'I_6.gph" "`outfigdir'`s'I_7.gph", scheme(s1color) iscale(0.5)
gr export "`outfigdir'`s'figure5.eps", replace


erase "`outfigdir'`s'I_4.gph"
erase "`outfigdir'`s'I_5.gph"
erase "`outfigdir'`s'I_6.gph"
erase "`outfigdir'`s'I_7.gph"

}

clear all
